﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data.SqlClient;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using System.Web.Mvc;
using MvcWebRole1.Models;

namespace MvcWebRole1.Controllers
{
    public class ItemController : ApiController
    {

        // GET api/item/5
        public IEnumerable<ItemModel> GetAllItemsFromStore(int Id)
        {
            using (var conn = new SqlConnection(ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString))
            {
                conn.Open();
                using (SqlCommand command = new SqlCommand("SELECT * FROM Item INNER JOIN ItemType ON Item.ItemId=ItemType.Id WHERE Item.StoreId =" + Id, conn))
                using (SqlDataReader reader = command.ExecuteReader())
                {
                    List<ItemModel> itemModelList = new List<ItemModel>();
                    while (reader.Read())
                    {
                        ItemModel tempItemModel = new ItemModel();
                        tempItemModel.ID = reader.GetInt32(0).ToString();
                        tempItemModel.Name = reader.GetString(5);
                        tempItemModel.Type = reader.GetString(6);
                        tempItemModel.Price = reader.GetSqlMoney(2).ToString();
                        
                        //itemModelList.Add(new ItemModel() { ID = reader.GetInt32(0).ToString(), Name = reader.GetString(1), Type = reader.GetInt32(2).ToString(), Price = reader.GetInt32(3).ToString() });
                        //Console.WriteLine("{0} {1} {2}",
                        //reader.GetInt32(0), reader.GetString(1), reader.GetString(2));
                        //ViewBag.Message += @"\n" +  + ": " + reader.GetString(1);
                        itemModelList.Add(tempItemModel);
                    }
                    return itemModelList;
                }
            }            
            //return "value";
        }

        // POST api/item
        public IEnumerable<ItemModel> Post(ItemQueryModel inputProduct)
        {
            using (var conn = new SqlConnection(ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString))
            {
                conn.Open();
                using (SqlCommand command = new SqlCommand("SELECT * FROM Item Where StoreID = " + inputProduct.StoreID, conn))
                using (SqlDataReader reader = command.ExecuteReader())
                {
                    List<ItemModel> itemModelList = new List<ItemModel>();
                    while (reader.Read())
                    {
                        ItemModel tempItemModel = new ItemModel();
                        tempItemModel.ID = reader.GetInt32(0).ToString();
                        if (!reader.IsDBNull(1))
                        {
                            tempItemModel.Name = reader.GetString(1);
                        }
                        if (!reader.IsDBNull(2))
                        {
                            tempItemModel.Type = reader.GetInt32(2).ToString();
                        }
                        if (!reader.IsDBNull(3))
                        {
                            tempItemModel.Price = reader.GetSqlMoney(3).ToString();
                        }
                        //itemModelList.Add(new ItemModel() { ID = reader.GetInt32(0).ToString(), Name = reader.GetString(1), Type = reader.GetInt32(2).ToString(), Price = reader.GetInt32(3).ToString() });
                        //Console.WriteLine("{0} {1} {2}",
                        //reader.GetInt32(0), reader.GetString(1), reader.GetString(2));
                        //ViewBag.Message += @"\n" +  + ": " + reader.GetString(1);
                        itemModelList.Add(tempItemModel);
                    }
                    return itemModelList;
                }
            }
        }

        // PUT api/item/5
        public void Put(int id, string value)
        {
        }

        // DELETE api/item/5
        public void Delete(int id)
        {
        }
    }
}
